package com.google.code.rgbgoesbetween.gui;


import com.google.code.rgbgoesbetween.GlobalConstants;
import com.google.code.rgbgoesbetween.R;
import com.google.code.rgbgoesbetween.data.AccountStatus;
import com.google.code.rgbgoesbetween.services.DataProvider;
import com.google.code.rgbgoesbetween.services.SessionManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity implements OnClickListener {

	private TextView notificationsView;
	private EditText usernameView;
	private EditText passwordView;
	private Button submit;
	private Button register;
	
	private DataProvider dataProvider = DataProvider.getDataProvider();
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
	    dataProvider.init();
	    SessionManager.getSessionManager().init();
        			
        notificationsView = (TextView) findViewById(R.id.notifications_field);
        usernameView 	  = (EditText) findViewById(R.id.username_field);
        passwordView  	  = (EditText) findViewById(R.id.password_field);
        submit 		 	  = (Button)   findViewById(R.id.submit_button);
        register     	  = (Button)   findViewById(R.id.register_button);
        
        submit.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.submit_button) {	
			AccountStatus status = SessionManager.getSessionManager().login(
					usernameView.getText().toString(), passwordView.getText().toString());
			if (AccountStatus.ADMIN_MEMBER.equals(status) || 
					AccountStatus.STANDARD_MEMBER.equals(status)) {
				notificationsView.setText("");
				Intent i = new Intent(this, HomeActivity.class);
				i.putExtra(GlobalConstants.ACCOUNT_TYPE, status);
				this.startActivity(i);
			}
			else if (AccountStatus.LOCKED_MEMBER.equals(status)) {
				notificationsView.setText("Account Locked. Contact an Administrator");
			}
			else if (AccountStatus.MEMBER_DNE.equals(status)) {
				notificationsView.setText("Username/Password combo incorrect. " +
						"Please enter again or register an account");
			}
		}
		else if (view.getId() == R.id.register_button){
			Intent i = new Intent(this, RegisterActivity.class);
			this.startActivity(i);
		}
	}
       
}
